package yxy.game.pm2.module.chat;

import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

import java.util.HashSet;
import java.util.Set;

/**
 * 单人聊天黑名单模块
 *
 * @ClassName SingleChatModule
 * @Author jingtao-zheng
 * @Date 2022/8/15 16:13
 * @Version 1.0
 **/
public class SingleChatBalckModule extends BaseGameRedisBean {
    private String playerCode;
    private Set<String> blackSet;

    public Set<String> getBlackSet() {
        return blackSet;
    }

    public void setBlackSet(Set<String> blackSet) {
        this.blackSet = blackSet;
    }

    public String getPlayerCode() {
        return playerCode;
    }

    public void setPlayerCode(String playerCode) {
        this.playerCode = playerCode;
    }
    //==========================================func
    /**
     * 添加拉黑玩家code到set
     * @param playerCode - 被拉黑玩家的code
     */
    public void setPlayerCodeToBlackSet(String playerCode){
        SingleChatBalckModule singleChatBalckModule = get(getPlayerCode());
        Set<String> blackSet = singleChatBalckModule.getBlackSet();
        blackSet.add(playerCode);
        singleChatBalckModule.set();
    }

    /**
     * 移除拉黑玩家的code
     * @param playerCode
     */
    public boolean removePlayerCodeFromBlackSet(String playerCode){
        SingleChatBalckModule singleChatBalckModule = get(getPlayerCode());
        Set<String> blackSet = singleChatBalckModule.getBlackSet();
        boolean remove = blackSet.remove(playerCode);
        singleChatBalckModule.set();
        return remove;
    }



    //==========================================redis
    static public SingleChatBalckModule get(String playerCode){
        try {
            SingleChatBalckModule module = getv(key(playerCode));
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), SingleChatBalckModule.class);
                if(module != null){module.set();}//把数据同步到redis
            }
            if (module == null) {
                module = create(playerCode);
            }
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 为玩家初始化个人黑名单
     * @param playerCode
     */
    static private SingleChatBalckModule create(String playerCode) {
        SingleChatBalckModule module = new SingleChatBalckModule();
        module.setPlayerCode(playerCode);
        module.setBlackSet(new HashSet<>());
        module.set();
        return module;
    }

    public void set() {
        try {
            String key = key(getPlayerCode());
            setv(key, this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public String key(String playerCode) {
        return String.format("PLAYER:%s:CHAT:SINGLEBALCK", playerCode);
    }

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                SingleChatBalckModule module = getv(key(playerCode));
                if(module != null) MySQLCacheDAO.setV(key, module);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }
}
